package com.yc.demo.QueryBooks;

import com.yc.demo.util.DBHelper;

import java.util.*;

public class BookDao {

    public List<Map<String,Object>> list = new LinkedList<>();

//    public List<Map<String,Object>> queryBook (){
//        String sql = "select * from books";
//        return  DBHelper.selectListMap(sql);
//    }
    //组合条件查询
    public List<Map<String,Object>> queryBook (String name,String press,String data){
        String sql = "select * from books where 1=1 ";
        List<Object> params = new ArrayList<>();
        if (name!=null &&name.trim().isEmpty()==false){
            sql += " and bookname like concat('%',?,'%') ";
            params.add(name);
        }
        if (press!=null &&press.trim().isEmpty()==false){
            sql += " and bookpress like concat('%',?,'%') ";
            params.add(press);
        }
        if (data!=null &&data.trim().isEmpty()==false){
            sql += " and pressdate = ? ";
            params.add(data);
        }
        return  DBHelper.selectListMap(sql,params.toArray());
    }

    public Map<String,Object> getBook(String id){
        String sql = "select * from books where id = ?";
        return  DBHelper.selectOneMap(sql,id);
    }

    public void save(String id,String name,String press,String date,String author,String count){
        String sql ="update books set bookname = ?,bookpress = ?,pressdate = ?,bookauthor = ?,bookcount =? where id = ?";
        DBHelper.update(sql,name,press, date,author,count,id);

    }
}
